//
//  WSTopEdgeView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/22.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//  帽子🎩

#import "WSTopEdgeView.h"
@interface WSTopEdgeView()
/**div1背景 白色 实现上下间隙  */
@property (nonatomic, strong) UIView *div1;

/**1.送餐方式*/
@property (nonatomic, strong) UILabel *songcanFangShiLb;
/**外送按钮*/
@property (nonatomic, strong) UIButton *waisongBtn;
/**自提按钮*/
@property (nonatomic, strong) UIButton *ziTiBtn;

@end

@implementation WSTopEdgeView

#pragma mark - 一 init

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        self.backgroundColor = kMainLightGrayColor; //灰色
    }
    return self;
}

- (void)setupView {
    
    [self addSubview:self.div1];
    
    [self.div1 addSubview:self.songcanFangShiLb];
    [self.div1 addSubview:self.waisongBtn];
    [self.div1 addSubview:self.ziTiBtn];
    //设置按钮间距
}

#pragma mark - 二 getter
- (UIView *)div1 {
    if (!_div1) {
        _div1 = [UIView new];
        _div1.backgroundColor = [UIColor whiteColor];
    }
    return _div1;
}
- (UILabel *)songcanFangShiLb {
    if (!_songcanFangShiLb) {
        _songcanFangShiLb = [UILabel new];
        //国际化
        NSString *deliveryType = NSLocalizedString(DeliveryType, @"送餐方式");
        _songcanFangShiLb.text = deliveryType ;
        
        _songcanFangShiLb.textColor = kLightGrayTextColor;
    }
    return _songcanFangShiLb;
}

- (UIButton *)waisongBtn {
    if (!_waisongBtn) {
        _waisongBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_waisongBtn addTarget:self action:@selector(clickWaiSongAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        //        @"外送"
        NSString *wsStr = NSLocalizedString(Home_Delivery, @"外送");
        [_waisongBtn setTitle:wsStr forState:(UIControlStateNormal)];
        [_waisongBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_waisongBtn setImage:[UIImage imageNamed:@"unselected"] forState:(UIControlStateNormal)];
        [_waisongBtn setImage:[UIImage imageNamed:@"selected"] forState:(UIControlStateSelected)];
//        _waisongBtn.selected = YES;//默认选中
        //2019-2-15 默认不允许选中
        
    }
    return _waisongBtn;
}
- (UIButton *)ziTiBtn {
    if (!_ziTiBtn) {
        _ziTiBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_ziTiBtn addTarget:self action:@selector(clickZiTiAction:) forControlEvents:(UIControlEventTouchUpInside)];
        //        @"自提"
        //国际化
        NSString *ztStr = NSLocalizedString(Home_SelfLifting, @"自提");
        
        [_ziTiBtn setTitle:ztStr forState:(UIControlStateNormal)];
        [_ziTiBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_ziTiBtn setImage:[UIImage imageNamed:@"unselected"] forState:(UIControlStateNormal)];
        [_ziTiBtn setImage:[UIImage imageNamed:@"selected"] forState:(UIControlStateSelected)];
    }
    return _ziTiBtn;
}

#pragma mark - 三 layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    //self h: 60
    //div1 h:40
    [self.div1 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.mas_centerY);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(40);
    }];
    
    [self layoutDiv1];
}


- (void)layoutDiv1 {
    
    //计算外送 自提文字长度
    CGFloat img_w = 20; //圆形按钮文字固定长度为 20
    CGFloat h = 20;
    
    //国际l
    NSString *wsStr = NSLocalizedString(Home_Delivery, @"外送");
    NSString *ztStr = NSLocalizedString(Home_SelfLifting, @"自提");
    
    CGSize wsSize = [wsStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0]}];
    CGFloat ws_w = wsSize.width * 1.2 + img_w + 5; //放大到1.2倍
    
    CGSize ztSize = [ztStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0]}];
    CGFloat zt_w = ztSize.width *1.2 + img_w + 5;
    
    
    [self.songcanFangShiLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.div1.mas_left).offset(15);
        make.centerY.equalTo(self.div1.mas_centerY);
        make.height.mas_equalTo(30);
        make.width.mas_greaterThanOrEqualTo(100); //2019-2-22国际化适配显示不全
//        make.size.mas_equalTo(CGSizeMake(100, 30));
    }];
    
    [self.ziTiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
//        make.right.equalTo(self.div1.mas_right).offset(-10); //距离右边10
        make.right.equalTo(self.div1.mas_right).offset(0); //距离右边10

        make.size.mas_equalTo(CGSizeMake(zt_w*1.2, h));//1-3宽度放大
        make.centerY.equalTo(self.div1.mas_centerY);
    }];
    
    [self.waisongBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(CGSizeMake(ws_w*1.2, h));
        make.right.equalTo(self.ziTiBtn.mas_left); //左边挨着右边
        make.size.mas_equalTo(CGSizeMake(ws_w, h));
    }];

    
    [self.ziTiBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [self.waisongBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
}


#pragma mark - 四 点击事件 clickWaiSongAction
- (void)clickWaiSongAction:(UIButton *)btn
{
    FDLog(@"外送");

    if (self.clickWaiSongBtnBlcok) {
        self.clickWaiSongBtnBlcok();
    }
    
    
    //逻辑判断， 只有一个被选中，始终都是选中状态
    btn.selected = YES;
    self.ziTiBtn.selected = NO;
}

- (void)clickZiTiAction:(UIButton *)btn
{
    btn.selected = YES;
    self.waisongBtn.selected = NO;

    FDLog(@"自提");
    if (self.clickZiTiBtnBlock) {
        self.clickZiTiBtnBlock();
    }
}
@end
